/*
 * Created on 17 �.�. 2547
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.softplus.carrefour.hd.bltier.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import java.text.DecimalFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import com.softplus.carrefour.CFException;
import com.softplus.carrefour.InfrastructureException;
import com.softplus.carrefour.hd.bltier.db.HibernateUtil;
import com.softplus.carrefour.hd.bltier.entity.Timezone;
import com.softplus.carrefour.hd.bltier.entity.UserAccount;
import com.softplus.carrefour.hd.bltier.entity.UserLevel;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

/**
 * @author Kowit
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DateUtil {
    public static Date getLocalDate(String userID) throws CFException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Timestamp tm = null;
        try {
            String sql = 
                "SELECT (SYSDATE + (SELECT time_shift" + "                     FROM TIMEZONE" + 
                "                    WHERE country_id = ua.country_id) / 24) LOCAL_DATE" + 
                "  FROM user_account ua" + " WHERE user_id = '" + userID + "'";

            conn = HibernateUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                tm = rs.getTimestamp("LOCAL_DATE");
            }
            stmt.close();
        } catch (InfrastructureException e) {
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }
        if (tm == null)
            return null;
        return new Date(tm.getTime());

    }

    public static Date getServerDate() throws CFException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Timestamp tm = null;
        try {
            String sql = "select sysdate CD from dual";
            conn = HibernateUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                tm = rs.getTimestamp("CD");
            }
            stmt.close();
        } catch (InfrastructureException e) {
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }

        return new Date(tm.getTime());
    }

    public static boolean isSameDay(Date d1, Date d2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal1.clear();
        cal2.clear();

        cal1.setTime(d1);
        cal2.setTime(d2);

        cal1.clear(Calendar.HOUR);
        cal1.clear(Calendar.HOUR_OF_DAY);
        cal1.clear(Calendar.MINUTE);
        cal1.clear(Calendar.SECOND);
        cal1.clear(Calendar.MILLISECOND);

        cal2.clear(Calendar.HOUR);
        cal2.clear(Calendar.HOUR_OF_DAY);
        cal2.clear(Calendar.MINUTE);
        cal2.clear(Calendar.SECOND);
        cal2.clear(Calendar.MILLISECOND);

        return cal1.getTime().getTime() == cal2.getTime().getTime();
    }

    public static boolean isNextDay(Date d1, Date d2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal1.clear();
        cal2.clear();

        cal1.setTime(d1);
        cal1.add(Calendar.DAY_OF_MONTH, 1);
        cal2.setTime(d2);


        cal1.clear(Calendar.HOUR);
        cal1.clear(Calendar.HOUR_OF_DAY);
        cal1.clear(Calendar.MINUTE);
        cal1.clear(Calendar.SECOND);
        cal1.clear(Calendar.MILLISECOND);

        cal2.clear(Calendar.HOUR);
        cal2.clear(Calendar.HOUR_OF_DAY);
        cal2.clear(Calendar.MINUTE);
        cal2.clear(Calendar.SECOND);
        cal2.clear(Calendar.MILLISECOND);

        return cal1.getTime().getTime() == cal2.getTime().getTime();
    }

    public static String elapsedTimeToString(long diff) {

        String time = null;

        //diff = diff%(1000*60*60*24);
        long hour = diff / (1000 * 60 * 60);
        diff = diff % (1000 * 60 * 60);
        long mint = diff / (1000 * 60);
        diff = diff % (1000 * 60);
        long sec = diff / (1000);


        time = hour + " : " + mint;

        return time;
    }

    public static Date addTime(Date d1, String time) {

        GregorianCalendar cal = new GregorianCalendar();
        cal.clear();
        cal.setTime(d1);

        int hh = 0;
        int mm = 0;


        final int len = time.length();
        hh = Integer.parseInt(time.substring(0, len - 2));
        mm = Integer.parseInt(time.substring(len - 2, len));

        cal.add(Calendar.HOUR_OF_DAY, hh);
        cal.add(Calendar.MINUTE, mm);

        return cal.getTime();
    }

    public static Date setTime(Date d1, String time) {

        GregorianCalendar cal = new GregorianCalendar();
        cal.clear();
        cal.setTime(d1);

        cal.clear(Calendar.HOUR);
        cal.clear(Calendar.HOUR_OF_DAY);
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);

        int hh = 0;
        int mm = 0;


        final int len = time.length();
        hh = Integer.parseInt(time.substring(0, len - 2));
        mm = Integer.parseInt(time.substring(len - 2, len));

        cal.add(Calendar.HOUR_OF_DAY, hh);
        cal.add(Calendar.MINUTE, mm);

        return cal.getTime();
    }

    public static long getDurationTime(String time) {

        long du = 0;
        int hh = 0;
        int mm = 0;


        final int len = time.length();
        hh = Integer.parseInt(time.substring(0, len - 2));
        mm = Integer.parseInt(time.substring(len - 2, len));

        du = hh * 1000 * 60 * 60;
        du += mm * 1000 * 60;
        System.out.println("Hr : " + hh);
        System.out.println("Mn : " + mm);
        return du;
    }

    public static long getWorkingDurationTime(String start, String end) {

        long du = 0;
        int hh = 0;
        int mm = 0;
        GregorianCalendar cal = new GregorianCalendar();
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);

        Date startTime = null;
        Date endTime = null;

        hh = Integer.parseInt(start.substring(0, 2));
        mm = Integer.parseInt(start.substring(2, 4));

        cal.set(Calendar.HOUR_OF_DAY, hh);
        cal.set(Calendar.MINUTE, mm);

        startTime = cal.getTime();

        hh = Integer.parseInt(end.substring(0, 2));
        mm = Integer.parseInt(end.substring(2, 4));

        cal.set(Calendar.HOUR_OF_DAY, hh);
        cal.set(Calendar.MINUTE, mm);

        endTime = cal.getTime();

        return endTime.getTime() - startTime.getTime();
    }

    public static long compareDate(Date d1, Date d2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal1.clear();
        cal2.clear();

        cal1.setTime(d1);
        cal2.setTime(d2);


        cal1.clear(Calendar.HOUR);
        cal1.clear(Calendar.HOUR_OF_DAY);
        cal1.clear(Calendar.MINUTE);
        cal1.clear(Calendar.SECOND);
        cal1.clear(Calendar.MILLISECOND);

        cal2.clear(Calendar.HOUR);
        cal2.clear(Calendar.HOUR_OF_DAY);
        cal2.clear(Calendar.MINUTE);
        cal2.clear(Calendar.SECOND);
        cal2.clear(Calendar.MILLISECOND);

        return (cal2.getTime().getTime() - cal1.getTime().getTime()) / 
            (1000 * 60 * 60 * 24);
    }

}
